<?php
class WxWebUser extends CWebUser
{
    use BasicTrait;

    public function authLogin($UserModel)
    {
        $this->changeIdentity($UserModel->id_user, 'newretail_wxuser', null);

        Yii::app()->getSession()->regenerateID();

        $data = $UserModel->attributes;

        $this->setData($data);
        return !$this->getIsLogin();
    }

    protected function afterLogout()
    {
        $this->setData(array());
        Yii::app()->getSession()->regenerateID();
    }

    public function setData($value)
    {
        $this->setState('__data', $value);
    }

    protected $_data = null;

    public function getData($key = null)
    {
        $data = $this->getState('__data');

        if(empty($data['id_user'])){
            return $data;
        }

        if($this->_data === null){
            $data = NewretailUser::model()->findByPk($data['id_user']);
            if(!empty($data)){
                $this->_data = $data->attributes;
            }

            $this->setData($this->_data);
        }

        if($key){
            return isset($this->_data[$key])? $this->_data[$key] : null;
        }

        return $this->_data;
    }

    public function getDbData($key = null, $refresh = true)
    {
        $user = $this->getData();
        if(empty($user)){
            return null;
        }
        $data = NewretailUser::model()->findByPk($user['id_user']);
        if($refresh){
            $d = array();
            if($data){
                $d = $data->attributes;
            }

            $this->setData($d);
        }
        if($key){
            $data = $data->$key;
        }

        return $data;
    }

    public function getAnyone()
    {
        return true;
    }

    public function getIsVisitor()
    {
        return !$this->IsLogin;
    }

    public function getIsLogin()
    {//return true;
        $user = $this->getData();

        if(empty($user)){
            return false;
        }
        return true;
    }
}